﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Model.Query;
using Labthink.PCSoft.UniversalHost4.UserControls;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.Instrument.Forms
{
    public partial class FrmSendRecord 
    {
        private QueryBase _qb;
        private NotifyManager _notify;
        public FrmSendRecord()
        {
            InitializeComponent();
            _qb = new QueryBase { PageCount = 10, PageIndex = 1 };
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
          
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            double height = SystemParameters.FullPrimaryScreenHeight;
            double width = SystemParameters.FullPrimaryScreenWidth;

            Width = width * 0.9;

            Top = (height - Height) / 2;
            Left = (width - Width) / 2;
             _notify = NotifyManager.InstanceManager(this);
            Loading.Visibility = Visibility.Visible;
            pager.OnPageIndexChanged += (s, a) =>
            {
                _qb.PageIndex = pager.PageIndex;
                _getRecords();
            };
            _getRecords();
        }

        private void BtnQuery_OnClick(object sender, RoutedEventArgs e)
        {
            _qb.PageIndex = 1;
            _getRecords();
        }

        private void BtnReset_OnClick(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);
            var r = DataShieldClient.DeleteRecords();
            if (r < 0)
            {
                _notify.ShowError(LanguageManager.GetLanguageValue("ActionFail"));
            }
            else
            {
                _notify.ShowSuccess(LanguageManager.GetLanguageValue("ActionSuccess"));
                LogRepository.AddLog(
                     LanguageManager.GetLanguageValue("OP-DeleteSendRecords"), "Data");
            }
            _triggerButtonsStatus(true);
            BtnQuery_OnClick(null, null);
        }

        private void BtnDeleteRow_OnClick(object sender, RoutedEventArgs e)
        {
            if (sender is Button btn)
            {
                var fn = btn.Tag + "";
                var r = DataShieldClient.DeleteRecord(fn);
                if (r < 0)
                {
                    _notify.ShowError(LanguageManager.GetLanguageValue("ActionFail"));
                }
                else
                {
                    _notify.ShowSuccess(LanguageManager.GetLanguageValue("ActionSuccess"));
                    LogRepository.AddLog(
                        LanguageManager.GetLanguageValue("OP-DeleteSendRecord",fn), "Data");
                }
                _triggerButtonsStatus(true);
                BtnQuery_OnClick(null, null);
            }
        }

        private void _getRecords()
        {
            _triggerButtonsStatus(false);
            GridData.ItemsSource = null;
            var datas = DataShieldClient.GetRecords(_qb);
            
            if (datas.InnerList == null || !datas.InnerList.Any())
            {
                pager.TotalItemCount = 0;
                pager.TotalPageCount = 0;
                pager.PageIndex = 0;
            }
            else
            {
                GridData.ItemsSource = datas.InnerList;
                pager.TotalItemCount = datas.TotalCount;
                pager.TotalPageCount = datas.PageCount;
                pager.PageIndex = _qb.PageIndex;
            }

            _triggerButtonsStatus(true);

        }

        private void _triggerButtonsStatus(bool status)
        {
            Dispatcher?.Invoke(() =>
            {
                Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;

                BtnQuery.IsEnabled = status;
                BtnReset.IsEnabled = status;
            });
        }
    }
}
